{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup as bs\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re  \n",
    "import requests  \n",
    "\n",
    "def dowmloadPic(html,keyword , i ):  \n",
    "    pic_url = re.findall('\"objURL\":\"(.*?)\",',html,re.S)     \n",
    "    print '找到关键词:'+keyword+'的图片，现在开始下载图片...'  \n",
    "    for each in pic_url:  \n",
    "        print u'正在下载第'+str(i+1)+u'张图片，图片地址:'+str(each)  \n",
    "        try:  \n",
    "            pic= requests.get(each, timeout=50)  \n",
    "        except  Exception,ex :  \n",
    "            print u'【错误】当前图片无法下载'   \n",
    "            continue  \n",
    "        string = 'pictures\\\\'+keyword+'_'+str(i) + '.jpg'  \n",
    "        #resolve the problem of encode, make sure that chinese name could be store  \n",
    "        fp = open(string.decode('utf-8').encode('cp936'),'wb')  \n",
    "        fp.write(pic.content)  \n",
    "        fp.close()  \n",
    "        i += 1  \n",
    "    return i  \n",
    "  \n",
    "if __name__ == '__main__':  \n",
    "    word =  raw_input('Input keywords:')  \n",
    "    word = word.decode('cp936').encode('utf-8')  \n",
    "    #url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word='+word+'&ct=201326592&v=flip'  \n",
    "    pnMax = input('Input max pn:')  \n",
    "    pncount = 0  \n",
    "    gsm = 80  #这个值不知干嘛的  \n",
    "    str_gsm =str(gsm)  \n",
    "    if not os.path.exists('pictures'):  \n",
    "        os.mkdir('pictures')  \n",
    "    while pncount<pnMax:  \n",
    "        str_pn = str(pncount)  \n",
    "        url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word='+word+'&pn='+str_pn+'&gsm='+str_gsm+'&ct=&ic=0&lm=-1&width=0&height=0'  \n",
    "        result = requests.get(url)  \n",
    "        pncount = dowmloadPic(result.text,word ,pncount)  \n",
    "    print u'下载完毕'  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:anaconda3]",
   "language": "python",
   "name": "conda-env-anaconda3-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
